<?php

class UrlManager
{
    public static function ValidateUrl($url)
    {
        return $url == str_replace(array("?", "$", "@", "%", "^", "!", "&"), "_", $url);
    }
    
    public static function ParseUrl($url)
    {
        $goodLink = UrlManager::ValidateUrl($url);
        
        if(substr_count($url, "http://") == 0)
        {
            if(substr_count($url, START_PAGE) == 0)
            {
                if($url{0} == '/')
                {
                    $url = START_PAGE . $url;
                }
                else
                {
                    $url = START_PAGE . "/" . $url;
                }
            }
        }
        
        if(substr_count($url, "../") != 0)
        {
            $url = str_replace("../", "", $url);
        }
        
        if($url{0} == '#')
        {
            $goodLink = FALSE;
        }
        
        if(substr_count($url, "#") != 0)
        {
            $url = substr($url, 0, strpos($url, "#"));
        }
        
        return $goodLink ? $url : "";       
    }
    
    public static function EditUrls($matches, $url)
    {
        for($i = 0; $i < count($matches[1]); $i++)
        {
            if(substr_count($matches[1][$i], "/") == 0)
            {
                if((substr_count($matches[1][$i], ISSUE) != 0)||(substr_count($matches[1][$i], MSG) != 0)||(substr_count($matches[1][$i], FILE))||(substr_count($matches[1][$i], USER)))
                {
                    $matches[1][$i] = BUGS_LINK . $matches[1][$i];
                }
                else
                {
                    if(substr_count($url, "/") == 1)
                    {
                        if(substr_count($url, ".html") == 0)
                        {
                            $matches[1][$i] = $url . "/" . $matches[1][$i];
                        }
                        else
                        {
                            $matches[1][$i] = substr($url, 0, strpos($url, "/")) . "/" . $matches[1][$i];
                        }
                    }
                    if(substr_count($url, "/") == 2)
                    {
                        $matches[1][$i] = substr($url, strpos($url, "/") + 1, strrpos($url, "/") - strpos($url, "/") - 1) . "/" . $matches[1][$i];
                    }
                }
            }
        }
        return $matches[1];        
    } 
    
    public static function GetMainBlockContent($pageContent)
    {
        $startPos = strpos($pageContent, START_OF_BLOCK) + START_POS_PADDING;
        $len = (strpos($pageContent, END_OF_BLOCK) + END_POS_PADDING) - $startPos;
        return substr($pageContent, $startPos, $len);
    }
    
    public static function GetClient($url)
    {
        $client = curl_init();
        curl_setopt($client, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($client, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($client, CURLOPT_URL, $url);
        return $client;
    }
}

?>